﻿using ConfigInfoWebPlatform.API.ViewModel;
using ConfigInfoWebPlatform.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.DBCommon
{
    /// <summary>
    /// 操作人员信息
    /// </summary>
    public class DAL_UserInfo
    {
        private IFreeSql freeSql;

        /// <summary>
        /// 操作人员信息
        /// </summary>
        public DAL_UserInfo()
        {
            freeSql = FreesqlFactory.GetFreeSql("configplatform");
        }

        /// <summary>
        /// 获取用户的信息
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public UserInfoModel GetUser(UserInfoViewModel viewModel)
        {
            // 查询配置文件信息
            var query = freeSql.Select<PersonInfo>()
                             .Where(x => x.Name == viewModel.Name)
                             .WhereIf(viewModel.IsVerifyPassword == true, x => x.Password == viewModel.Password)
                             .ToOne(x => new UserInfoModel
                             {
                                 Id = x.Id,
                                 D_id = x.DId,
                                 G_id = x.GId,
                                 R_id = x.RId,
                                 Name = x.Name,
                                 Xianshimingcheng = x.Xianshimingcheng
                             });
            return query;
        }

        /// <summary>
        /// 根据用户Id获取角色Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetUserRid(string id)
        {
            string rid = string.Empty;
            var user = freeSql.Select<PersonInfo>()
                            .Where(x => x.Id == id)
                            .ToOne();
            //首先判断是否绑定角色，如果没有绑定角色通过组织表获取对应的角色Id
            if(user!=null && !string.IsNullOrWhiteSpace(user.RId))
            {
                rid = user.RId;
            }
            else
            {
                if(!string.IsNullOrWhiteSpace(user.GId))
                {
                    rid = freeSql.Select<Organization>()
                               .Where(x => x.Id == user.GId)
                               .ToOne(x => x.RId);
                }
            }
            return rid;
        }

        /// <summary>
        /// 获取用户列表数据
        /// </summary>
        /// <returns></returns>
        public List<UserListModel> GetUserList(string name,string did,bool?isAdmin)
        {
            var query = freeSql.Select<PersonInfo, Department, Organization, Role>()
                             .LeftJoin((a, b, c, d) => a.DId.Replace("-", "") == b.Id)
                             .LeftJoin((a, b, c, d) => a.GId == c.Id)
                             .LeftJoin((a,b,c,d)=>a.RId==d.Id)
                             .WhereIf(!string.IsNullOrWhiteSpace(name), (a, b, c, d) => a.Name.Contains(name))
                             .WhereIf(isAdmin == false, (a, b, c, d) => a.DId == did)
                             .ToList((a, b, c, d) => new UserListModel
                             {
                                 Id = a.Id,
                                 Name = a.Name,
                                 Xianshimingcheng = a.Xianshimingcheng,
                                 Did = b.Id,
                                 Dname = b.Name,
                                 Gid = a.GId,
                                 Gname = c.Name,
                                 Rid = a.RId,
                                 Rname = d.Name
                             });
            return query;
        }

        /// <summary>
        /// 获取组织详情信息
        /// </summary>
        /// <param name="id">组织Id</param>
        /// <returns></returns>
        public UserInfoModel GetUserDetail(string id)
        {
            var query = freeSql.Select<PersonInfo>()
                             .Where(x => x.Id == id)
                             .ToOne(x => new UserInfoModel()
                             {
                                 Id = x.Id,
                                 D_id = x.DId,
                                 G_id = x.GId,
                                 R_id = x.RId,
                                 Name = x.Name,
                                 Xianshimingcheng = x.Xianshimingcheng
                             });
            return query;
        }

        /// <summary>
        /// 修改人员角色信息
        /// </summary>
        public int EditUserRoleInfo(string id, string rid)
        {
            var result = freeSql.Update<PersonInfo>()
                              .Set(x => x.RId, rid)
                              .Where(x => x.Id == id)
                              .ExecuteAffrows();
            return result;
        }


        /// <summary>
        /// 根据用户名获取部门Id
        /// </summary>
        public string GetUserDidByUserName(string userName)
        {
            var query = freeSql.Select<PersonInfo>()
                             .Where(x => x.Name == userName)
                             .ToOne(x => x.DId);
            return query;
        }

    }
}
